package com.carleasoft.mps.cms.business.resource.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carleasoft.mps.cms.business.resource.bean.entity.ReasourceCollectEntity;
import com.carleasoft.mps.cms.business.resource.bean.po.ReasourcePageQueryPo;
import com.carleasoft.mps.cms.business.resource.bean.po.ReasourcePo;
import com.carleasoft.mps.cms.business.resource.bean.po.ResourcePo;
import com.carleasoft.mps.cms.business.resource.bean.vo.*;
import com.carleasoft.mps.cms.business.resource.service.ReasourceCollectService;
import com.carleasoft.mps.cms.business.resource.service.ReasourceService;
import com.carleasoft.mps.core.result.R;
import com.carleasoft.mps.core.utils.IBeanUtil;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 资源八库
 *
 * @author: yangHao
 * @createDate:2023/5/18
 * @description:
 */
@RestController
@RequestMapping("/resource")
public class ReasourceController {

    @Autowired
    private ReasourceService reasourceService;
    @Autowired
    private ReasourceCollectService reasourceCollectService;

    /**
     * 教学资源八库-分页
     *
     * @param reasourcePageQueryPo
     * @param request
     * @return
     */
    @PostMapping("/pageQuery")
    public R pageQuery(@RequestBody ReasourcePageQueryPo reasourcePageQueryPo, HttpServletRequest request) {
        Page<ReasourcePageQueryVo> reasourcePageQueryVoPage = reasourceService.pageQuery(new Page(reasourcePageQueryPo.getCurrent(), reasourcePageQueryPo.getSize()), reasourcePageQueryPo, request);
        return R.ok(reasourcePageQueryVoPage);
    }


    /**
     * 教学资源八库-新增
     *
     * @param reasourcePo
     * @param request
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody ReasourcePo reasourcePo, HttpServletRequest request) {
        boolean isOk = reasourceService.saveResource(reasourcePo, request);
        return R.ok(isOk);
    }

    /**
     * 教学资源八库-编辑
     *
     * @param reasourcePo
     * @param request
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody ReasourcePo reasourcePo, HttpServletRequest request) {
        IBeanUtil.paramVerify(reasourcePo, "reasourceCode");
        boolean isOk = reasourceService.updateResource(reasourcePo, request);
        return R.ok(isOk);
    }


    /**
     * 教学资源八库-置顶
     *
     * @param reasourcePo
     * @return
     */
    @PostMapping("/top")
    public R top(@RequestBody ReasourcePo reasourcePo) {
        IBeanUtil.paramVerify(reasourcePo, "reasourceCode", "topType");
        boolean isOk = reasourceService.top(reasourcePo);
        return R.ok(isOk);
    }

    /**
     * 教学资源八库-删除
     *
     * @param reasourcePo
     * @return
     */
    @PostMapping("/delete")
    public R delete(@RequestBody ReasourcePo reasourcePo) {
        IBeanUtil.paramVerify(reasourcePo, "reasourceCode");
        boolean isOk = reasourceService.delete(reasourcePo);
        return R.ok(isOk);
    }

    /**
     * 教学资源八库-详情
     *
     * @param reasourcePo
     * @return
     */
    @PostMapping("/detail")
    public R detail(@RequestBody ReasourcePo reasourcePo) {
        IBeanUtil.paramVerify(reasourcePo, "reasourceCode");
        ReasourceVo reasourceVo = reasourceService.detail(reasourcePo);
        return R.ok(reasourceVo);
    }

    /**
     * 教学资源八库-新增分类
     *
     * @param reasourceCollectEntity
     * @param request
     * @return
     */
    @PostMapping("/saveCategory")
    public R saveCategory(@RequestBody ReasourceCollectEntity reasourceCollectEntity, HttpServletRequest request) {
        boolean isOk = reasourceCollectService.saveCategory(reasourceCollectEntity, request);
        return R.ok(isOk);
    }

    /**
     * 教学资源八库-幂等检测 查询分类是否名字重复
     *
     * @param reasourceCollectEntity
     * @return
     */
    @PostMapping("/check")
    public R check(@RequestBody ReasourceCollectEntity reasourceCollectEntity) {
        IBeanUtil.paramVerify(reasourceCollectEntity, "categoryName", "library");
        boolean isOk = reasourceCollectService.check(reasourceCollectEntity);
        return R.ok(isOk);
    }

    /**
     * 教学资源八库-删除
     *
     * @param reasourceCollectEntity
     * @return
     */
    @PostMapping("/deleteCategory")
    public R deleteCategory(@RequestBody ReasourceCollectEntity reasourceCollectEntity) {
        IBeanUtil.paramVerify(reasourceCollectEntity, "categoryCode");
        boolean isOk = reasourceCollectService.deleteCategory(reasourceCollectEntity);
        return R.ok(isOk);
    }


    /**
     * 教学资源八库-分类列表
     *
     * @return
     */
    @PostMapping("/categoryList")
    public R categoryList() {
        List<ReasourceCollectEntity> reasourceCollectEntities = reasourceCollectService.categoryList();
        return R.ok(reasourceCollectEntities);
    }


    /**
     * 根据所属库查询分类数据
     *
     * @return
     */
    @PostMapping("/getLibraryByCategory")
    public R getLibraryByCategory(@RequestBody ReasourcePo reasourcePo) {
        IBeanUtil.paramVerify(reasourcePo, "library");
        List<CategoryVo> categoryVos = reasourceService.getLibraryByCategory(reasourcePo);
        return R.ok(categoryVos);
    }

    /**
     * 门户 收藏/取消收藏/浏览/下载
     *
     * @param reasourcePo
     * @return
     */
    @PostMapping("/viewDetail")
    public R viewDetail(@RequestBody ReasourcePo reasourcePo, HttpServletRequest request) {
        IBeanUtil.paramVerify(reasourcePo, "reasourceCode", "openType");
        boolean isOk = reasourceService.viewDetail(reasourcePo, request);
        return R.ok(isOk);
    }

    /**
     * 资源概况
     *
     * @return
     */
    @PostMapping("/workbenchCount")
    public R workbenchCount() {
        WorkbenchCountVo workbenchCountVo = reasourceService.workbenchCount();
        return R.ok(workbenchCountVo);
    }


    /**
     * web-教学资源概况
     * @return
     */
    @PostMapping("/webCount")
    public R webCount() {
        List<WebCountVo> webCountVos = reasourceService.webCount();
        return R.ok(webCountVos);
    }


    /**
     * 导出资源八库
     * @param
     * @return
     */
    @PostMapping("/importExcel")
    public R importExcel(@RequestBody ResourcePo resourcePo , HttpServletResponse response){
      boolean isOk=  reasourceService.importExcel(resourcePo,response);
      return R.ok(isOk);
    }

}
